package org.wx.io.netty.tcp.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.concurrent.EventExecutorGroup;
import lombok.extern.slf4j.Slf4j;

/**
 * @description: tcp 编码器
 * @author: wangxun
 * @createDate: 2021/2/22
 * @version: 1.0
 */
@Slf4j
public class TcpEncoder extends MessageToByteEncoder<MyTcpProtocol> {
    private int count = 0;
    @Override
    protected void encode(ChannelHandlerContext ctx, MyTcpProtocol msg, ByteBuf out) throws Exception {
      log.info("TcpEncoder 编码器开始编码，将java对象转化成byte字节 次数{}",++this.count);
      out.writeByte(msg.getCommand());
      out.writeInt(msg.getPackageLength());
      out.writeBytes(msg.getContent());
    }
}
